package com.tbit.main.service.handler;

import com.tbit.main.annos.PreAuthorize;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author jing
 * @date 2021/3/2514:18
 * @Description:
 */
@Aspect
@Component
@Slf4j
public class PreAuthorizeAspect {

    /**
     * 对标注有operateLog注解的进行拦截,@annotation表示标注了某个注解的所有方法。
     */
    @Pointcut("@annotation(com.tbit.main.annos.PreAuthorize)")
    public void preAuthorizeAspect() {

    }

    @Before("preAuthorizeAspect()")
    public void doBefore(JoinPoint point) {
        handlPreAuthorize(point);
    }

    protected void handlPreAuthorize(final JoinPoint joinPoint) {
        // 获得注解
        PreAuthorize preAuthorize = getAnnotationLog(joinPoint);
        if (preAuthorize == null) {
            return;
        }
    }


    /**
     * 是否存在注解，如果存在就获取
     */
    private PreAuthorize getAnnotationLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null) {
            return method.getAnnotation(PreAuthorize.class);
        }
        return null;
    }


}
